package com.example.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: 21050134
 * @date: 2023/12/11 15:36
 * @description:
 */
@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler) throws Exception{
// 拦截器，我们也可以获取请求的url、参数等。也可以获取返回结果。
		log.info("interceptor  pre begin..");
		String token = request.getHeader("token");
		if(token != null && token.equals("1234")){
			log.info("login success");
			//return true 继续往下走，调用下一个拦截器
			return true;
		}

		return false;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception{
		log.info("interceptor  post begin..");
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) throws Exception{
		log.info("interceptor  afterCompletion");
	}
}
